{% extends 'base/base.html' %}
{% load users_tags %}
{% load mytags %}
{% load static %}
{% block content %}
    <div class="row">
        <div class="clearfix"></div>
        <div class="row">
            <div class="col-md-12 col-sm-12 col-xs-12">
                <div class="x_panel">
                    <div class="x_title">
                        <h2>用户管理
                        <small>编辑用户</small>
                        </h2>
                        <ul class="nav navbar-right panel_toolbox">
                            <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a></li>
                            <li><a class="close-link"><i class="fa fa-close"></i></a></li>
                        </ul>
                        <div class="clearfix"></div>
                    </div>
                    <div class="x_content">
                    {% for row in form %}
                        <form class="form-horizontal form-border" method="post" action="/user_edit-{{ aid }}/" enctype="multipart/form-data">
                            {% csrf_token %}

                                <div class="item form-group">
                                    <label class="control-label col-md-2 col-sm-2 col-xs-12" style="text-align:left;">{{ row.label_tag }}</label>
                                    <div class="col-md-6 col-sm-6 col-xs-12">
                                        {{ row }}
                                        <div style="padding-top:10px;">
                                            {{ row.errors }}
                                        </div>
                                    </div>
                                </div>
                            {% endfor %}

                            <!--用户组权限左右边框start-->
                            <div class="ln_solid"></div>
                            <div id="show_department" class="row">
                                {% show_user_groups aid %}
                            </div>
                            <div class="ln_solid"></div>
                            <div id="show_perm" class="row">
                                {% if aid|is_super %}
                                    <fieldset disabled>{% else %}
                                    <fieldset>{% endif %}
                                {% show_permissions aid 'user' %}
                                </fieldset>
                            </div>
                            <div class="ln_solid"></div>
                            <select hidden="hidden" id="group_sel" name="group_sel" size="8"
                                    multiple="multiple"></select>
                            <select hidden="hidden" id="group_del" name="group_del" size="8"
                                    multiple="multiple"></select>
                            <select hidden="hidden" id="perm_sel" name="perm_sel" size="8" multiple="multiple"></select>
                            <select hidden="hidden" id="perm_del" name="perm_del" size="8" multiple="multiple"></select>
                            <div class="form-group">
                            <!--用户组权限左右边框end-->
                                <div>
                                    <button id="send" type="submit" class="btn btn-primary pull-left">提交</button>
                                    <button type="reset" class="btn btn-success pull-left" onclick="res();">返回</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}

{% block js %}
    <!-- validator -->
    <script type="text/javascript" src="/static/js/validator.min.js"></script>
    <script type="text/javascript" src="/static/js/multiselect.min.js"></script>
    <script>
        function res() {
            history.go(-1);
        }
    </script>

    <script>
        jQuery(document).ready(function ($) {
            $('#search').multiselect({
                search: {
                    left: '<input type="text" name="q" class="form-control" placeholder="Search..." />',
                    right: '<input type="text" name="q" class="form-control" placeholder="Search..." />',
                }
            });
            $('#search_group').multiselect({
                search: {
                    left: '<input type="text" name="q" class="form-control" placeholder="Search..." />',
                    right: '<input type="text" name="q" class="form-control" placeholder="Search..." />',
                }
            });
        });

    </script>

        <!-- validator -->
    <script>
      // initialize the validator function
      validator.message.date = 'not a real date';

      // validate a field on "blur" event, a 'select' on 'change' event & a '.reuired' classed multifield on 'keyup':
      $('form')
        .on('blur', 'input[required], input.optional, select.required', validator.checkField)
        .on('change', 'select.required', validator.checkField)
        .on('keypress', 'input[required][pattern]', validator.keypress);

      $('.multi.required').on('keyup blur', 'input', function() {
        validator.checkField.apply($(this).siblings().last()[0]);
      });

      $('form').submit(function(e) {
        e.preventDefault();
        var submit = true;

        // evaluate the form using generic validaing
        if (!validator.checkAll($(this))) {
          submit = false;
        }

        if (submit) {
            $("#search_group_to option[name='user_group']").each(function(i){
            if(this.value){
            $("#group_sel").append("<option selected='selected' value='"+this.value+"'>"+this.text+"</option>");
            }
            });
            $("#search_group option[name='user_group_select']").each(function(i){
            if(this.value){
            $("#group_del").append("<option selected='selected' value='"+this.value+"'>"+this.text+"</option>");
            }
            });
            $("#search_to option[name='permission']").each(function(i){
            if(this.value){
            $("#perm_sel").append("<option selected='selected' value='"+this.value+"'>"+this.text+"</option>");
            }
            });
            $("#search option[name='permission_select']").each(function(i){
            if(this.value){
            $("#perm_del").append("<option selected='selected' value='"+this.value+"'>"+this.text+"</option>");
            }
            });
            this.submit();
        }

        return false;
      });
    </script>
    <!-- /validator -->


{% endblock %}